/*
	The sshlib should be used for terminal emulator.
*/
#include <sshlib.h>


#ifdef SSHTERM_EXPORTS
#define SSHTERM_API __declspec(dllexport)
#else
#define SSHTERM_API __declspec(dllimport)
#endif


#define FONT_NORMAL 0
#define FONT_BOLD 1
#define FONT_UNDERLINE 2
#define FONT_BOLDUND 3
#define FONT_WIDE	0x04
#define FONT_HIGH	0x08
#define FONT_NARROW	0x10

#define FONT_OEM 	0x20
#define FONT_OEMBOLD 	0x21
#define FONT_OEMUND 	0x22
#define FONT_OEMBOLDUND 0x23

#define FONT_MAXNO 	0x2F
#define FONT_SHIFT	5

#define NCFGCOLOURS 22
#define NEXTCOLOURS 240
#define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
struct FontSpec {
    char name[64];
    int isbold;
    int height;
    int charset;
};

struct term_config_tag {
    /* Basic options for SSH backend. */
	sshConfig *cfg;

    char localusername[100];
    int rfc_environ;
    int passive_telnet;
    /* Serial port options */
    char serline[256];
    int serspeed;
    int serdatabits, serstopbits;
    int serparity;
    int serflow;
    /* Keyboard options */
    int bksp_is_delete;
    int rxvt_homeend;
    int funky_type;
    int no_applic_c;		       /* totally disable app cursor keys */
    int no_applic_k;		       /* totally disable app keypad */
    int no_mouse_rep;		       /* totally disable mouse reporting */
    int no_remote_resize;	       /* disable remote resizing */
    int no_alt_screen;		       /* disable alternate screen */
    int no_remote_wintitle;	       /* disable remote retitling */
    int no_dbackspace;		       /* disable destructive backspace */
    int no_remote_charset;	       /* disable remote charset config */
    int remote_qtitle_action;	       /* remote win title query action */
    int app_cursor;
    int app_keypad;
    int nethack_keypad;
    int telnet_keyboard;
    int telnet_newline;
    int alt_f4;			       /* is it special? */
    int alt_space;		       /* is it special? */
    int alt_only;		       /* is it special? */
    int localecho;
    int localedit;
    int alwaysontop;
    int fullscreenonaltenter;
    int scroll_on_key;
    int scroll_on_disp;
    int erase_to_scrollback;
    int compose_key;
    int ctrlaltkeys;
    char wintitle[256];		       /* initial window title */
    /* Terminal options */
    int savelines;
    int dec_om;
    int wrap_mode;
    int lfhascr;
    int cursor_type;		       /* 0=block 1=underline 2=vertical */
    int blink_cur;
    int beep;
    int beep_ind;
    int bellovl;		       /* bell overload protection active? */
    int bellovl_n;		       /* number of bells to cause overload */
    int bellovl_t;		       /* time interval for overload (seconds) */
    int bellovl_s;		       /* period of silence to re-enable bell (s) */
    Filename bell_wavefile;
    int scrollbar;
    int scrollbar_in_fullscreen;
    int resize_action;
    int bce;
    int blinktext;
    int win_name_always;
    FontSpec font;
    int font_quality;
    Filename logfilename;
    int logtype;
    int logxfovr;
    int logflush;
    int logomitpass;
    int logomitdata;
    int hide_mouseptr;
    int sunken_edge;
    int window_border;
    char answerback[256];
    char printer[128];
    int arabicshaping;
    int bidi;
    /* Colour options */
    int ansi_colour;
    int xterm_256_colour;
    int system_colour;
    int try_palette;
    int bold_colour;
    unsigned char colours[22][3];
    /* Selection options */
    int mouse_is_xterm;
    int rect_select;
    int rawcnp;
    int rtf_paste;
    int mouse_override;
    short wordness[256];
    /* translations */
    int vtmode;
    char line_codepage[128];
    int cjk_ambig_wide;
    int utf8_override;
    int xlat_capslockcyr;
    /* SSH bug compatibility modes */
    int sshbug_ignore1, sshbug_plainpw1, sshbug_rsa1,
	sshbug_hmac2, sshbug_derivekey2, sshbug_rsapad2,
	sshbug_pksessid2, sshbug_rekey2;
    /* Options for pterm. Should split out into platform-dependent part. */
    int stamp_utmp;
    int login_shell;
    int scrollbar_on_left;
    int shadowbold;
    FontSpec boldfont;
    FontSpec widefont;
    FontSpec wideboldfont;
    int shadowboldoffset;
};


enum {
    SER_PAR_NONE, SER_PAR_ODD, SER_PAR_EVEN, SER_PAR_MARK, SER_PAR_SPACE
};
enum {
    SER_FLOW_NONE, SER_FLOW_XONXOFF, SER_FLOW_RTSCTS, SER_FLOW_DSRDTR
};

#ifndef SSHTERM_DLL
typedef void *SshWindow;

typedef struct term_config_tag Config;


/* ---------- EXPORTS ------------ */

/* init_config() */
typedef void (*TERM_INIT_CONFIG_PROC)(Config *cfg);

/* term_init() */
typedef BOOL (*TERM_INIT_PROC)(Backend *back, HWND hwnd, Config cfg, SshWindow *handle) ;

/* start_session() */
typedef BOOL (*TERM_START_SESSION_PROC)(SshWindow handle, void *backhandle, int channel) ;

/* term_wndproc() */
typedef int (*TERM_WNDPROC_PROC)(SshWindow wnd, UINT message, WPARAM wParam, LPARAM lParam);

/* term_data() */
typedef int (*TERM_DATA_PROC)(SshWindow wnd, int is_stderr, const char *data, int len);

/*
	term_wndproc is to be called to handle almost all window messages, except WM_CLOSE, WM_DESTROY and WM_QUIT
	These should be processed in an application (which should call close_session() first)
	all backend workaround should be done inside an application too. sshterm_wndproc returns TRUE if message was 
	handled, FALSE if it was not handled. In last case, default WndProc should be called.
	term_data() should be called from it in response of SSH_CALLBACK_CHANNEL_DATA callback.
	Most of work with terminal window (including key press and mouse events handling) is done inside sshterm.
	Menu, window creating/closing, getting userpass input is not handled.
	Helper function for Config structure filling is provided. It sets default values, which application can change later 
	if need.

	remote_exit() exported function SHOULD be called upon callback from SSH backend (notify_remote_exit() proc)

	to start frontend successfully, following things should be completed:
	1. Create main window and define DefWindowProc for it. It should call sshterm_wndproc() exported from this DLL 
		to handle (almost) all window messages
	2. Init cfg structure for the backend
	3. Init cfg structure for the frontend (sshterm). Frontend's cfg.cfg is a pointer to backend's cfg
	4. Load the backend. Get backend pointer
	5. call term_init() exported from this DLL. Specify backend pointer, window handle, cfg and pointer 
		where to store SshWindow handle. It prepares window for output.
	6. Define SSH_CHANNEL_DATA ,SSH_DISCONNECT, get_user_input() callback functions. 
		SSH_CHANNEL_DATA shoud call	term_data() function. SSH_DISCONNECT is to be handled by application, 
		get_user_input() shall handle user/password requests from backend.
	7. Start the backend. Receive backend handle and channel ID.
	8. call start_session() exported from this DLL and specify given SshWindow handle, given backend handle and given channel ID.
		this will connect frontend and backend.

	Hints: you can use helper functions for filling cfg structures with default values. Or write your own initialization script.
	Application can call close_session() when remote host is disconnected or application want to close window.


*/

#endif
